import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/club/req_club.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/custom_toast.dart';
import 'package:flutter_changdian_football/utils/event/custom_event_util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class AnnouncementController extends GetxController {

  AnnouncementController({
    required this.clubId
  });

  final String clubId;

  TextEditingController bulletinController = TextEditingController();

  /// 设置俱乐部公告
  setClubBulletin(p) async {
    if (bulletinController.text.trim() == '') {
      return ;
    }

    try {
      EasyLoading.show();

      var resp = await ReqClub.setClubBulletin(
        clubId: p,
        bulletin: bulletinController.text,
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          CustomToast.showTextToast('发布成功');
          Get.until((route) => Get.currentRoute == AppRoutes.main);
          eventBus.fire(const BottomNavigationBarEvent(CustomConstant.EVENT_CLUB_REFRESH));
        } else {
          CustomToast.showTextToast(resp.data['msg']);
        }
      } else {
        CustomToast.showTextToast('系统繁忙，请稍后再试！');
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }


  @override
  void onInit() {
    super.onInit();

    init();
  }

  init() async {
    var resp = await ReqClub.getClubBulletin(
      clubId: clubId,
    );
    if (resp.status == Status.completed) {
      if (resp.data['code'] == '200') {
        bulletinController.text = resp.data['data']['bulletin'];
        update();
      }
    }
  }

}